var express = require('express')
var app = express()
var router = express.Router()
var request = require('request')

router.get('/api/**', function (req, res, next) {
  var url = req.originalUrl
  url = 'http://localhost:8083/' + url

  var body = req.body
  var token = req.cookies && req.cookies['Authorition']
  for (var p in body) {
    if (!body[p] || body[p] == '') {
      delete body[p]
    }
  }
  var head = {
    'authorization': token
  }
  var opts = {
    headers: head,
    url: 'http://www.163.com',
    json: true,
    form: body
  }

  request.get(opts, function (err, resp, data) {
    res.send(data)
  })
})

router.post('/api/**', function (req, res) {
  var url = req.originalUrl
  url = 'http://localhost:8083' + url

  var body = req.body
  var token = req.cookies && req.cookies['Authorition']
  for (var p in body) {
    if (!body[p] || body[p] == '') {
      delete body[p]
    }
  }
  var head = {
    'authorization': token
  }
  var opts = {
    headers: head,
    url: url,
    json: true,
    form: body
  }

  request.post(opts, function (err, resp, data) {
    res.send(data)
  })
})

module.exports = router
